<?php
class Dashboard extends CI_Model {
	
	function __construct() {
		parent::__construct (); 
		$this->load->database();
	}
	//track js
	public function trcJS(){
		if(isset($_POST['trcjs'])){
			$arr_tmp['trcjs'] = $_POST['trcjs'];
			$content = serialize($arr_tmp);
			$time = date('Y-m-d H:i:s');
			$sql = "insert into lz_tits(`tit`,`language`,`addtime`,`content`,`module` )values(
						'trcjs','cn','{$time}','{$content}','trcjs'),(
						'trcjs','en','{$time}','{$content}','trcjs')";
			$this->db->query($sql);//,`flag`
			
			echo '<script >
			location.reload();
			</script>';
			exit;
		}else{
			$sql = "select * from lz_tits where `tit`='trcjs' and `language`='cn' Limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$tmp = $query->row();
				$data['trcjs'] = unserialize($tmp->content);
				$this->load->vars($data);
			}
		}
	}
	//上传图片
	public function uploadPic(){
		$data['error'] = $data['rt']  = '';
		if(isset($_POST['uploadpic'])){
			$config['upload_path'] = './resources/image/sys/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '80';
			$config['max_width']  = '800';
			$config['max_height']  = '768';
			$this->load->library('upload', $config); 
			$rt = $this->upload->do_upload();
			if ( !$rt )
			{
				$data['error'] = $this->upload->display_errors();
			}else{
				$data['rt'] = $rt;
			} 
		}
		$this->load->vars($data);
	}
	//更新缓存
	public function updateCache($args){
		$l = isset($_POST['l'])?$_POST['l']:'';
		$module = isset($_POST['mo'])?$_POST['mo']:''; 
		$limit = 1;
		if($module == 'navi'){
			$limit = 2;
		}
		if($module == 'course'){
			$limit = 5;
		}
		if($module == 'knowledge'){
			$limit = 4;
		}
		if($module != '' && (in_array($l, array('cn','en')))){
				$params[0] = $l;
				$this->load->library('simple_cache', $params);
				$sql = "select * from lz_tits where `language`='{$params[0]}' and `module`='{$module}'  order by `id` desc limit {$limit}";
			 
				$query = $this->db->query($sql);
				if ($query->num_rows() > 0){
					foreach ($query->result() as $k=>$v){
						$content = unserialize($v->content);
						$tit = $v->tit; 
						//file_put_contents($tit.'.php', var_export($content, true));
						if( $this->simple_cache->isCached($tit) ){
							$this->simple_cache->deleteItem($tit);
						}
						$this->simple_cache->cacheItem($tit,$content);
					}
				}
			die('1');
		}else{
			die('0');
		}
			
	}
	
	//汉语知识详细管理
	function m_knowledge(){	
		$data = array();
		if(count($_POST)){
			if(isset($_POST['cn'])){
				$language = 'cn';
				unset($_POST['cn']);
			}
			if(isset($_POST['en'])){
				$language = 'en';
				unset($_POST['en']);
			}
		
			$knowledge_titles = isset($_POST['knowledge_title'])?$_POST['knowledge_title']:'';
			$knowledge_contents = isset($_POST['knowledge_content'])?$_POST['knowledge_content']:'';
			 
			$sql1 = '';
			$time = date('Y-m-d H:i:s'); 
			foreach ($knowledge_titles as $k=>$v){
				if(trim($v)){
					$tmp_key = 'knowledge_tit';
					$tmp_key=$tmp_key.$k;
					$content = serialize(array($v,$knowledge_contents[$k]));
					$sql1.="( '{$tmp_key}','{$language}','{$time}','{$content}','knowledge' ),";
				}
				continue;
			}
			$sql1 = substr($sql1,0,-1);
			$sql = "insert into lz_tits(`tit`,`language`,`addtime`,`content`,`module`)values{$sql1}";
			$this->db->query($sql);
			echo '<script >
			location.reload();
			</script>';
			exit;
		}else{
			$sql = "SELECT
				*
					FROM
					lz_tits
					WHERE `module` = 'knowledge'
					AND `language` = 'cn'
					AND `addtime` =
					(
					SELECT
					MAX(`addtime`)
					FROM
					lz_tits WHERE `module` = 'knowledge'
					AND `language` = 'cn')  order by `id` LIMIT 5";
					$query = $this->db->query($sql);
					if ($query->num_rows() > 0){
						$data['knowledge_cn'] = $query->result();
					}
					$sql = "SELECT
					*
					FROM
					lz_tits
					WHERE `module` = 'knowledge'
					AND `language` = 'en'
					AND `addtime` =
					(
					SELECT
					MAX(`addtime`)
					FROM
					lz_tits WHERE `module` = 'knowledge'
					AND `language` = 'en')  order by `id` LIMIT 5";
					$query = $this->db->query($sql);
					if ($query->num_rows() > 0){
						$data['knowledge_en'] = $query->result();
					}
				}
				$this->load->vars($data);
	}
	
	//课程详细管理
	function m_course(){
		$data = array();
		if(count($_POST)){
			if(isset($_POST['cn'])){
				$language = 'cn';
				unset($_POST['cn']);
			}
			if(isset($_POST['en'])){
				$language = 'en';
				unset($_POST['en']);
			}
		
			$course_titles = isset($_POST['course_title'])?$_POST['course_title']:'';
			$course_contents = isset($_POST['course_content'])?$_POST['course_content']:'';
			file_put_contents('t.php', var_export($_POST ,true));
			$sql1 = '';
			$time = date('Y-m-d H:i:s');
			
			
			foreach ($course_titles as $k=>$v){
				if(trim($v)){
					$tmp_key = 'course_tit';
					$tmp_key=$tmp_key.$k;
					$content = serialize(array($v,$course_contents[$k]));
					$sql1.="( '{$tmp_key}','{$language}','{$time}','{$content}','course' ),"; 
				}
				continue;
			}
			$sql1 = substr($sql1,0,-1);
			$sql = "insert into lz_tits(`tit`,`language`,`addtime`,`content`,`module`)values{$sql1}";
			$this->db->query($sql);
			echo '<script >
			location.reload();
			</script>';
			exit;
		}else{ 
			$sql = "SELECT 
  * 
FROM
  lz_tits 
WHERE `module` = 'course' 
  AND `language` = 'cn' 
  AND `addtime` = 
  (
	SELECT 
	MAX(`addtime`) 
	FROM
	lz_tits WHERE `module` = 'course' 
    AND `language` = 'cn')  order by `id` LIMIT 5";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$data['course_cn'] = $query->result();
			}  
			$sql = "SELECT 
  * 
FROM
  lz_tits 
WHERE `module` = 'course' 
  AND `language` = 'en' 
  AND `addtime` = 
  (
	SELECT 
	MAX(`addtime`) 
	FROM
	lz_tits WHERE `module` = 'course' 
    AND `language` = 'en')  order by `id` LIMIT 5";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$data['course_en'] = $query->result();
			}
		}
		$this->load->vars($data);
	}
	//联系方式管理
	function m_contact(){
		$data = array();
		if(count($_POST)){
			if(isset($_POST['cn'])){
				$language = 'cn';
				unset($_POST['cn']);
			}
			if(isset($_POST['en'])){
				$language = 'en';
				unset($_POST['en']);
			}
		
			$arr_tmp['contact_title'] = isset($_POST['contact_title'])?$_POST['contact_title']:'';
			$names = isset($_POST['names'])?$_POST['names']:'';
			$values = isset($_POST['values'])?$_POST['values']:'';
			 foreach ($names as $k=>$v){
			 	if(trim($v)){
			 		$arr_tmp[$k] = array($v,$values[$k]);
			 	}
			 	continue;
			 } 
			$time = date('Y-m-d H:i:s');
			$content = serialize($arr_tmp);
			$sql = "insert into lz_tits(`tit`,`language`,`addtime`,`content`,`module`)values(
			'contact','{$language}','{$time}','{$content}','contact' )";
			$this->db->query($sql);
			echo '<script >
			location.reload();
			</script>';
			exit;
		}else{
			$sql = "select * from lz_tits where `tit`='contact' and `language`='cn' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$tmp = $query->row();
				$data['contact_cn'] = unserialize($tmp->content);
			}
			$sql = "select * from lz_tits where `tit`='contact' and `language`='en' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$tmp = $query->row();
				$data['contact_en'] = unserialize($tmp->content);
			}
		}
		$this->load->vars($data);
	}
	//首页focus管理
	function m_indexFocus(){
		$data = array();
		if(count($_POST)){
			if(isset($_POST['cn'])){
				$language = 'cn';
				unset($_POST['cn']);
			}
			if(isset($_POST['en'])){
				$language = 'en';
				unset($_POST['en']);
			}
		
			$arr_tmp['index_bigtit'] = isset($_POST['index_bigtit'])?$_POST['index_bigtit']:'';
			$arr_tmp['index_runidea'] = isset($_POST['index_runidea'])?$_POST['index_runidea']:'';
			$arr_tmp['index_runideac'] = isset($_POST['index_runideac'])?$_POST['index_runideac']:'';
			$arr_tmp['index_devidea'] = isset($_POST['index_devidea'])?$_POST['index_devidea']:'';
			$arr_tmp['index_devideac'] = isset($_POST['index_devideac'])?$_POST['index_devideac']:'';
			$arr_tmp['index_wish'] = isset($_POST['index_wish'])?$_POST['index_wish']:'';
			$arr_tmp['index_wishc'] = isset($_POST['index_wishc'])?$_POST['index_wishc']:'';
			$time = date('Y-m-d H:i:s');
			$content = serialize($arr_tmp);
			$sql = "insert into lz_tits(`tit`,`language`,`addtime`,`content`,`module`)values(
			'indexfocus','{$language}','{$time}','{$content}','indexfocus' )";
			$this->db->query($sql);
			echo '<script >
			location.reload();
			</script>';
			exit;
		}else{
			$sql = "select * from lz_tits where `tit`='indexfocus' and `language`='cn' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
			$tmp = $query->row();
				$data['indexfocus_cn'] = unserialize($tmp->content);
			}
			$sql = "select * from lz_tits where `tit`='indexfocus' and `language`='en' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
			$tmp = $query->row();
			$data['indexfocus_en'] = unserialize($tmp->content);
			}
		}
		$this->load->vars($data);
	}
	//首页middle三块管理
	function m_indexMiddle(){
		$data = array();
		if(count($_POST)){
			if(isset($_POST['cn'])){
				$language = 'cn';
				unset($_POST['cn']);
			}
			if(isset($_POST['en'])){
				$language = 'en';
				unset($_POST['en']);
			}
			$arr_tmp = array();
			$index_middletits = isset($_POST['index_middletit'])?$_POST['index_middletit']:'';
			$index_middleurls = isset($_POST['index_middleurl'])?$_POST['index_middleurl']:'';
			$index_middlecs = isset($_POST['index_middlec'])?$_POST['index_middlec']:'';
			foreach($index_middletits as $k=>$v){
				$arr_tmp[$k] = array('index_middletit'=>$v,
														   'index_middleurl'=>$index_middleurls[$k],
															'index_middlec'=>$index_middlecs[$k]);
			}
			$time = date('Y-m-d H:i:s');
			$content = serialize($arr_tmp);
			$sql = "insert into lz_tits(`tit`,`language`,`addtime`,`content`,`module`)values(
			'indexmiddle','{$language}','{$time}','{$content}','indexmiddle' )";
			$this->db->query($sql);
			echo '<script >
			location.reload();
			</script>';
			exit;
		}else{
			$sql = "select * from lz_tits where `tit`='indexmiddle' and `language`='cn' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$tmp = $query->row();
				$data['indexmiddle_cn'] = unserialize($tmp->content);
			}
			$sql = "select * from lz_tits where `tit`='indexmiddle' and `language`='en' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$tmp = $query->row();
				$data['indexmiddle_en'] = unserialize($tmp->content);
			}
		}
		$this->load->vars($data);
	}
	//公司简介管理
	function m_about(){
		$data = array();
		if(count($_POST)){
			if(isset($_POST['cn'])){
				$language = 'cn';
				unset($_POST['cn']);
			}
			if(isset($_POST['en'])){
				$language = 'en';
				unset($_POST['en']);
			}
			 
			$arr_tmp['about_title'] = isset($_POST['about_title'])?$_POST['about_title']:'';
		    $arr_tmp['about_content'] = isset($_POST['about_content'])?$_POST['about_content']:'';
			$time = date('Y-m-d H:i:s'); 
			$content = serialize($arr_tmp);
			$sql = "insert into lz_tits(`tit`,`language`,`addtime`,`content`,`module`)values(
			'about','{$language}','{$time}','{$content}','about' )";
			$this->db->query($sql); 
			echo '<script >
			location.reload();
			</script>';
			exit;
		}else{
			$sql = "select * from lz_tits where `tit`='about' and `language`='cn' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$tmp = $query->row();
				$data['about_cn'] = unserialize($tmp->content);
			}
			$sql = "select * from lz_tits where `tit`='about' and `language`='en' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$tmp = $query->row();
				$data['about_en'] = unserialize($tmp->content);
			}
		}
		$this->load->vars($data);
	}
	//每日一句列表
	function m_dailyonelist(){
		$data = array();
		$sql = "select * from lz_tits where `tit`='dailyone' and `language`='cn' order by `id` desc";
		$query = $this->db->query($sql);
		/* foreach($query->result() as $row){
			$tmp = $query->row();
			$data['dailyone_cn'] = unserialize($tmp->content);
		} */
		$data['query'] = $query;
		$this->load->vars($data);
	}
	//每日一句
	function m_dailyone(){
		 
		if(count($_POST)){
			/* if(isset($_POST['cn'])){
				$language = 'cn';
				unset($_POST['cn']);
			}
			if(isset($_POST['en'])){
				$language = 'en';
				unset($_POST['en']);
			} */
			$id = isset($_POST['id'])?$_POST['id']:'0';
			$arr_tmp['pinyin'] = isset($_POST['pinyin'])?$_POST['pinyin']:'';
			$arr_tmp['sentence'] = isset($_POST['sentence'])?$_POST['sentence']:'';
			$arr_tmp['pic'] = isset($_POST['pic'])?$_POST['pic']:'';
			$arr_tmp['explain'] = isset($_POST['explain'])?$_POST['explain']:'';
			$arr_tmp['en_explain'] = isset($_POST['en_explain'])?$_POST['en_explain']:'';
			if(count($arr_tmp['explain'] ) > 1){
				foreach( $arr_tmp['explain'] as $k => $v){
					if(trim($v) == ''){
						unset($arr_tmp['explain'][$k]);
					}
				}
			}
			$time = date('Y-m-d H:i:s');
			$content = addslashes(serialize($arr_tmp));
			//$flag = isset($_POST['flag'])?$_POST['flag']:0;
			if($id) {
				$sql = "UPDATE lz_tits set `tit`='dailyone',`language`='cn',`addtime`='{$time}',
							`content`='{$content}',`module`='dailyone' WHERE `id`='{$id}' LIMIT 1
				";//,`flag`='{$flag}'
			}else{
				$sql = "insert into lz_tits(`tit`,`language`,`addtime`,`content`,`module`)values(
				'dailyone','cn','{$time}','{$content}','dailyone' ),(
				'dailyone','en','{$time}','{$content}','dailyone' )";
			}//,'{$flag}',`flag`
			$this->db->query($sql);
			echo '<script >
			location.href="/index.php/admin/home/dailyonelist";
			</script>';
			exit;
		}else if(isset($_GET['id'])){
			$sql = "select * from lz_tits where id='{$_GET['id']}' LIMIT 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0) { 
				$tmp = $query->row(); 
				$data['dailyone_cn'] = unserialize($tmp->content);
				$data['dailyone_id'] = $tmp->id;
				$this->load->vars($data);
			} 
		}
	}
	//友情链接管理
	function m_friendLink( ){
		$data = array();
		if(count($_POST)){
			if(isset($_POST['cn'])){
				$language = 'cn';
				unset($_POST['cn']);
			}
			if(isset($_POST['en'])){
				$language = 'en';
				unset($_POST['en']);
			}
			 
			$flink_tits = isset($_POST['flink_tit'])?$_POST['flink_tit']:'';
			$flink_urls = isset($_POST['flink_url'])?$_POST['flink_url']:'';
			
			$time = date('Y-m-d H:i:s');
			if(!empty($flink_tits) || !empty($flink_urls)){
				$arr_tmp = array();
				for($i = 0; $i<count($flink_tits); $i++){
					if($flink_tits[$i] != '' && $flink_urls[$i] != ''){
						$arr_tmp[$i] = array('flink_tit'=>$flink_tits[$i], 'flink_url'=>$flink_urls[$i]);
					}
					continue;
				}
				$arr_tmp['link_title'] = $_POST['link_title'];
				$content = serialize($arr_tmp);
				$sql = "insert into lz_tits(`tit`,`language`,`addtime`,`content`,`module`)values(
			'flink','{$language}','{$time}','{$content}','flink' )";	
				$this->db->query($sql);
			} 
			echo '<script >
			 location.reload();
			</script>';
			exit;
		}else{
			$sql = "select * from lz_tits where `tit`='flink' and `language`='cn' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$tmp = $query->row();
				$data['flink_cn'] = unserialize($tmp->content);
			}
			$sql = "select * from lz_tits where `tit`='flink' and `language`='en' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$tmp = $query->row();
				$data['flink_en'] = unserialize($tmp->content);
			}
		}
		$this->load->vars($data); 
	}
	//header管理
	function m_naviTits($args){
		 $data = array();
		if(count($args)){
			if(isset($args['cn'])){
				$language = 'cn'; 
				unset($args['cn']);
			}
			if(isset($args['en'])){
				$language = 'en';
				unset($args['en']);
			}
			$time = date('Y-m-d H:i:s');
			
			$content = serialize($args['naviTits']);
			$args['subNaviTits'] = array('Course'=> $args['Course'], 'Knowledge'=>$args['Knowledge']);
			$content_sub = serialize($args['subNaviTits']);
			$sql = "insert into lz_tits(`tit`,`language`,`addtime`,`content`,`module`)values(
			'naviTits','{$language}','{$time}','{$content}','navi'
			),('subNaviTits','{$language}','{$time}','{$content_sub}','navi'
			)";
			$this->db->query($sql);
			echo '<script >
				parent.location.href="/index.php/admin/home";
			</script>';	
			exit;
		}else{ 
			$sql = "select * from lz_tits where `tit`='naviTits' and `language`='cn' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$tmp = $query->row();
				$data['naviTits_cn'] = unserialize($tmp->content);
			}
			$sql = "select * from lz_tits where `tit`='subNaviTits' and `language`='cn' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$tmp = $query->row();
				$data['subNaviTits_cn'] = unserialize($tmp->content); 
			}
			$sql = "select * from lz_tits where `tit`='naviTits' and `language`='en' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$tmp = $query->row();
				$data['naviTits_en'] = unserialize($tmp->content);
			}
			$sql = "select * from lz_tits where `tit`='subNaviTits' and `language`='en' order by `addtime` desc limit 1";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0){
				$tmp = $query->row();
				$data['subNaviTits_en'] = unserialize($tmp->content); 
			}
		}
	 
		$this->load->vars($data);
	}  
	function __destruct() {
	
	}
}
?>